/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.repository;

import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.Whfa;
import me.zhengjie.business.domain.Zhsq;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-04-15
**/
public interface ZhsqPgRepository extends JpaRepository<Zhsq, Long>, JpaSpecificationExecutor<Zhsq> {


    /**
     * 根据申请状态和评估人员ID查询申请信息
     * @param zt 状态
     * @param id 主键
     */
//    @Modifying
    @Query(value = "SELECT " +
            "zh.* " +
            "FROM " +
            "BIS_ZHSQ zh " +
            "LEFT JOIN BIS_FPRY fp ON zh.BIS_ZHSQ_ID = fp.BIS_FPRY_ZHSQ_ID  " +
            "WHERE " +
            "zh.BIS_ZHSQ_ZT = ?1  " +
            "AND fp.BIS_FPRY_RYGL_ID = ?2 " +
            "AND zh.BIS_ZHSQ_ZHLB = ?3  " +
            "AND (zh.bis_zhsq_snry_xm like concat(concat('%', ?4),'%') or zh.bis_zhsq_snry_sfz like concat(concat('%', ?4),'%') )  " +
            "AND fp.BIS_FPRY_ZT = '0' ",nativeQuery = true,
            countProjection = "1")
    Page<Zhsq> queryAllByPgUserId(String zt, Long id, String zhlb, String blurry, Pageable pageable);
    /**
     * 更改状态
     * @param zt 状态
     * @param id 主键
     */
    @Modifying
    @Query(value = "update bis_zhsq set bis_zhsq_zt = ?1 where bis_zhsq_id = ?2",nativeQuery = true)
    void updateZhzt(String zt, Long id);

    /**
     * 根据身份证查询
     * @param bisZhsqSnrySfz 身份证
     * @return /
     */
    Hljg findByBisZhsqSnrySfz(String bisZhsqSnrySfz);


    /**
     * 根据月份查询照护方案
     * @return /
     */
    @Query(value = "select r1.bis_zhsq_id, " +
            "       r1.bis_zhsq_snry_xm, " +
            "       r1.bis_zhsq_snry_xb, " +
            "       r1.bis_zhsq_snry_sfz, " +
            "       r1.bis_zhsq_zt, " +
            "       r1.bis_zhsq_adl, " +
            "       r1.bis_zhsq_lxdh, " +
            "       r1.bis_zhsq_hldz, " +
            "       r1.bis_zhsq_jhrxm, " +
            "       r1.bis_zhsq_jhrdh, " +
            "       r1.bis_zhsq_gx, " +
            "       r1.bis_zhsq_sqyy, " +
            "       r1.bis_zhsq_adl_pc, " +
            "       r3.bis_adl_xm_id, " +
            "       r3.bis_adl_xm_name, " +
            "       r3.bis_adl_xm_px, " +
            "       r4.bis_adl_xmxx_id, " +
            "       r4.bis_adl_xmxx_name, " +
            "       r4.bis_adl_xmxx_px, " +
            "       r4.bis_adl_xmxx_fs, " +
            "       case when r4.bis_adl_xmxx_id = r5.bis_adl_xmxx_id then 'selected' else '' end is_default " +
            "  from bis_zhsq r1 " +
            "  left join bis_adl_xm r3 on r1.bis_zhsq_adl_pc = r3.bis_adl_pc_id " +
            "  left join bis_adl_xmxx r4 on r3.bis_adl_xm_id = r4.bis_adl_xm_id " +
            "  left join bis_adl_zhsq r5 on r4.bis_adl_xmxx_id = r5.bis_adl_xmxx_id and r1.bis_zhsq_id = r5.bis_zhsq_id " +
            " where r1.bis_zhsq_id = ?1 " +
            " order by r3.bis_adl_xm_px, r4.bis_adl_xmxx_px ",nativeQuery = true)
    List<Object> findZhInfoAndAdlById(Long zhid);
}